package com.course.redis.handler;

import com.course.common.exception.BusinessException;
import com.course.common.exception.ValidatorException;
import com.course.common.response.BaseResponse;
import com.course.common.response.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @PROJECT_NAME: course
 * @DESCRIPTION: 全局异常处理
 * @Author: 涂玄武
 * @DATE: 2021/4/13 13:51
 */
@ControllerAdvice
public class ControllerExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);

    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public BaseResponse businessException(BusinessException e){
        logger.error("--业务异常：{}",e.getCode().getDesc());
        return new BaseResponse().failure(e.getCode().getDesc());
    }

    @ResponseBody
    @ExceptionHandler(value = ValidatorException.class)
    public BaseResponse validatorException(ValidatorException e){
        logger.error("--参数校验发生异常：",e.getMessage());
        return new BaseResponse(StatusCode.InvalidParams);
    }

}
